package com.huawei.mylibrary.dialog;


import com.huawei.mylibrary.ResourceTable;
import com.huawei.mylibrary.callback.PasswordCallback;
import com.huawei.mylibrary.utils.FingerprintToken;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;

public class PasswordDialog extends AnimatedDialog<PasswordDialog> {

    private FingerprintToken token;
    private PasswordCallback callback;

    private int passwordType;

//    public static final int PASSWORD_TYPE_TEXT = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
//    public static final int PASSWORD_TYPE_NUMBER = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD;

    private final static String TAG = "PasswordDialog";

    public PasswordDialog(Context context, FingerprintToken token) {
        super(context);
        this.token = token;
//        this.passwordType = PASSWORD_TYPE_TEXT;
        this.callback = null;
    }

    /**
     * Create a PasswordDialog instance.
     *
     * @param context Activity Context
     * @param token   Token got with FingerprintDialogSecureCallback
     * @return PasswordDialog instance
     */
    public static PasswordDialog initialize(Context context, FingerprintToken token) {
        return new PasswordDialog(context, token);
    }

    /**
     * Create a PasswordDialog instance.
     *
     * @param context Activity Context
     * @return PasswordDialog instance
     */
    public static PasswordDialog initialize(Context context) {
        return new PasswordDialog(context, null);
    }

    /**
     * Set callback triggered when Password is entered.
     *
     * @param callback The callback
     * @return PasswordDialog object
     */
    public PasswordDialog callback(PasswordCallback callback) {
        this.callback = callback;
        return this;
    }

    /**
     * Set the password type (text or numbers)
     *
     * @param passwordType PASSWORD_TYPE_TEXT or PASSWORD_TYPE_NUMBER
     * @return PasswordDialog object
     */
    public PasswordDialog passwordType(int passwordType) {
        this.passwordType = passwordType;
        return this;
    }


    public void show() {

        if (title == null || message == null) {
            throw new RuntimeException("Title or message cannot be null.");
        }

        dialogComponent = layoutScatter.parse(ResourceTable.Layout_password_dialog, null, false);
        ((Text) dialogComponent.findComponentById(ResourceTable.Id_password_dialog_title)).setText(title);
        ((Text) dialogComponent.findComponentById(ResourceTable.Id_password_dialog_message)).setText(message);

        final TextField input = (TextField) dialogComponent.findComponentById(ResourceTable.Id_password_dialog_input);
//        input.setInputType(passwordType);

        dialog.setContentCustomComponent(dialogComponent)
                .setButton(0, context.getString(ResourceTable.String_password_confirm), new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {

                    }
                })
                .setButton(1, context.getString(ResourceTable.String_password_cancel), new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog iDialog, int i) {

                    }
                })
                .show();


    }

}
